﻿@{
    ViewBag.Title = "模型扩展字段配置";
    Layout = "~/Views/Shared/_Main.cshtml";
}
<input type="hidden" id="hddModelTableID" value="@Model.ModelTableID" />
<style>
    .mss
    {
        cursor: pointer;
    }
    #editColumn
    {
        display: none;
    }
    .divGrid
    {
        max-height: 480px;
        overflow-y: scroll;
    }
    .version, .modelEdit
    {
        display: none;
    }
</style>
<script type="text/javascript">
    var UrlArray = $.getUrlArray();
    var retData = new Array();
    var opts = {
        height: 380,
        checkbox: true,
        rows: 500,
        url: "/ModelConfig/GetViewColumn",
        para: { modelID: UrlArray["modelID"], dbName: encodeURI(UrlArray["dbName"]), version: UrlArray["version"] },
        idField: "GUID",
        checkbox: true,
        columns: [
                { field: 'ColumnName', title: '字段名称', width: 20 },
                { field: 'ColumnDescription', title: '字段描述', width: 25 },
                { field: 'ColumnType', title: '字段类型', width: 13 },
                { field: 'PK', title: '主键', width: 7, align: 'center' },
                { field: 'ColumnIsNullable', title: '允许空', width: 7, align: 'center' },
                { field: 'ColumnFixValue', title: '固定值', width: 7, align: 'center' },
                { field: 'ColumnDefaultValue', title: '默认值', width: 7, align: 'center' },
                { field: 'ColumnExpression', title: '过滤器', width: 7, align: 'center', formatter: function (value, rowData, rowIndex) {
                    if (jv.IsNull(rowData.WhereCondition))
                        return "<img src='/images/silk/application_osx.png' onclick='EditViewColumn(\"" + rowIndex + "\",\"4\");' class='mss'/>";
                    else
                        return "<img src='/images/silk/application_osx_cascade.png' onclick='EditViewColumn(\"" + rowIndex + "\",\"4\");' class='mss'/>";
                }
                },
                 { field: 'ColumnScriptExtend', title: '脚本', align: 'center', width: 7, align: 'center', formatter: function (value, rowData, rowIndex) {
                     var add = true; ;
                     for (var j = 0; j < retData.length; j++) {
                         if (retData[j].GUID == rowData.GUID) {
                             add = false;
                         }
                     }
                     if (add) {
                         retData.push(rowData);
                     }
                     if (rowData.PK == "是")
                         return "";
                     else {
                         if (rowData.ColumnScriptExtend == "[" + rowData.ColumnName + "]")
                             return "<img src='/images/view01.png' onclick='EditViewScript(\"" + rowIndex + "\",\"2\");' class='mss'/>";
                         else
                             return "<img src='/images/view02.png' onclick='EditViewScript(\"" + rowIndex + "\",\"3\");' class='mss'/>";
                     }
                 }
                 }
            ],
        OnChickTR: function (value, rowData, rowIndex) {
            if (!UrlArray["version"]) {
                UpdateModel($.trim(rowIndex));
            }
        },
        OnLoaded: function () {
            $("#DataGrid  input").attr("checked", "checked");
        }
    };
    var urlPara;
    $(function () {
        urlPara = "?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"])
         + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
        if (UrlArray["version"]) {
            $(".version").show();
            urlPara = urlPara + "&version=" + UrlArray["version"];
            opts.checkbox = false;
        } else
            $(".modelEdit").show();
        $("#link a").each(function () {
            $(this).attr("href", $(this).attr("href") + urlPara);
        });
        $('#DataGrid').dg(opts);
    });
    function EditViewColumn(v, t) {
        var vcUrl = urlPara + "&id=" + v + "&type=" + t;
        if (UrlArray["version"]) {
            vcUrl = vcUrl + "&script=0&version=" + UrlArray["version"];
        }
        showModalMany("Info", "/ModelConfig/EditViewColumn" + vcUrl, "620", "280", returnData);
    }
    function EditViewScript(v, t) {
        var vsUrl = urlPara + "&id=" + v;
        if (UrlArray["version"]) {
            vsUrl = vsUrl + "&script=1&version=" + UrlArray["version"];
            showModalMany("Info", "/ModelConfig/EditViewColumn" + vsUrl, "620", "280", returnScriptData);
        } else
            showModalMany("Info", "/ModelConfig/EditViewScript" + vsUrl, "860", "560", returnScriptData);
    }
    function returnData(v, t) {
        if (v.GUID != "") {
            if (t == 2) {
                retData[v.GUID] = v;
            }
            retData[v.GUID]["ColumnIsModel"] = v["ColumnIsModel"];
            retData[v.GUID]["WhereCondition"] = v["WhereCondition"];
            retData[v.GUID]["Operator"] = v["Operator"];
            retData[v.GUID]["IsValue"] = v["IsValue"];
            retData[v.GUID]["ColumnExpression"] = v["ColumnExpression"];

        } else {
            v.GUID = retData.length;
            retData[v.GUID] = v;
        }
        var data = { "total": 1, "rows": retData };
        opts["data"] = data;
        $('#DataGrid').dg(opts);
    }
    function returnScriptData(v) {
        if (v.GUID != "") {
            retData[v.GUID].ColumnScriptExtend = v.ColumnScriptExtend;
            retData[v.GUID].ColumnType = v.ColumnType; 
            var data = { "total": 1, "rows": retData };
            opts["data"] = data;
            $('#DataGrid').dg(opts);
        }
    }
    function back() {
        window.location = "/ModelConfig/EditColumn?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"])
         + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
    }
    function save(n) {
        $("#msg").html("");
        var pkPass = false;
        for (var i = 0; i < retData.length; i++) {
            if (retData[i].PK == "是") {
                pkPass = true;
                break;
            }
        }
        if (!pkPass && n == "next") {
            alert("缺少主键，无法进行下一步操作。");
            return;
        }
        var modelID = UrlArray["modelID"];
        var dbName = UrlArray["dbName"];
        if (UrlArray["nmID"] && UrlArray["nmID"] != "null") {
            modelID = UrlArray["nmID"];
            dbName = UrlArray["ndbName"];
        }
        $("#loading").show();
        $.post("/ModelConfig/SaveExtendConfig", { modelID: modelID, data: $.toJSON(retData), dbName: encodeURI(dbName) }, function (result) {
            if (result == "1" || result == "") {
                if (n == "next")
                    goto();
                else if (n == "reload") {
                    alert("操作成功！列表将初始化。");
                    window.location.reload();
                }
                else
                    alert("操作成功！");
            }
            else if (result == "-1")
                alert("创建视图失败！");
            else
                $("#msg").html(result);

            $("#loading").hide();
        });
    } function goto() {
        window.location = "/ModelConfig/End?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"])
         + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
    }
    function gotoUrl(v) {
        window.location = v + urlPara;
    }
    function DeleteRow() {
        if (confirm("确定要删除吗?")) {
            var j = 1;
            var pass = false;
            var pkPass = true;
            $('#DataGrid').find(":checkbox").each(function (i, d) {
                if (i == 0)
                    return;
                if (d.checked) {
                    pass = true; 
                }
            });
            if (!pass) {
                alert("请选择要删除的行！");
                return;
            } else if (pkPass) {
                $('#DataGrid').find(":checkbox").each(function (i, d) {
                    if (i == 0)
                        return;
                    if (d.checked) {
                        retData.removeAt(i - j);
                        j++;
                    }
                });
                if (retData.length == 0) {
                    save('reload');
                }
                var data = { "total": 1, "rows": retData };
                opts["data"] = data;
                $("#DataGrid").dg(opts);
            }
        }
    }
</script>
<script type="text/javascript">
    var sysArr = new Array();
    var viewArr = {};
    var OperatorList = new Array();
    var IsModel = 0;
    function UpdateModel(id) {
        $("#editColumn").show();
        if (id != "") {
            retData[id].GUID = id;
            jv.SetDivEmpty(".searchArea", 4); //设置属性值 
            jv.SetDivJson(".searchArea", 4, retData[id]); //设置属性值  
            $("#txt_ColumnIsNullable").val("是");
            IsModel = retData[id].ColumnIsModel;
            if (retData[id].ColumnIsNullable == "是")
                $("#txt_ColumnIsNullable").attr("checked", "checked");
            else
                $("#txt_ColumnIsNullable").removeAttr("checked");
        } else {
            jv.SetDivEmpty(".searchArea", 4); //设置属性值 
        }
    }
    function saveView() {
        var columnName = $.trim($("#txt_ColumnName").val());
        var inColumn = new Array();
        inColumn.push("mdp_guid");
        inColumn.push("mdp_batchtime");
        inColumn.push("mdp_isdelete");
        inColumn.push("mdp_operationtype");
        if (columnName == "") {
            alert("字段名称不能为空，请添加字段名称！");
            return;
        }
        else if (inColumn.indexOf(columnName.toLowerCase()) > -1) {
            alert(columnName + "是系统字段，请使用其他字段名称！");
            return;
        }

        var toSave = true;
        if (IsModel == 1 && UrlArray["type"] == "2") {
            toSave = confirm("该字段为模型初始化字段，点击确定将修改为模型扩展字段。");
            if (toSave)
                IsModel = 0;
        }
        if (toSave) {
            var retArr = jv.GetDivArray(".searchArea", 4);
            for (var i = 0; i < retData.length; i++) {
                if (retArr["ColumnName"].toLowerCase() == retData[i].ColumnName.toLowerCase() && retArr["GUID"] != retData[i].GUID) {
                    alert("该字段名称已存在，请重新录入.");
                    return false;
                }
            }
            if (retArr["GUID"] == "") {
                retArr["PK"] = "否";
                retArr["ColumnType"] = "nvarchar(max)";
                retArr["ColumnLength"] = "-1";
            }
            retArr["ColumnIsModel"] = IsModel;
            if ($("#txt_ColumnIsNullable")[0].checked)
                retArr["ColumnIsNullable"] = "是";
            else
                retArr["ColumnIsNullable"] = "否";
            retArr["type"] = 1;

            if (jv.IsNull(retArr["ColumnScriptExtend"]))
                retArr["ColumnScriptExtend"] = retArr["ColumnName"];
            $("#editColumn").hide();
            returnData(retArr, 2);
            return false;
        }
    } 
</script>
<div style="margin: 5px;">
    <table class="listTitle" id="link">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="600">
                <div class="version">
                    <a href="/ModelConfig/Index">模型管理</a> --> <a href="/ModelConfig/Edit">查看模型渐变维度</a>
                    --> 模型扩展字段配置
                </div>
                <div class="modelEdit">
                    <a href="/ModelConfig/Index">模型管理</a> --> <a href="/ModelConfig/Edit">新增模型配置</a>
                    --> <a href="/ODSConfig/ODSConfigAdd">选择源系统表</a> --> <a href="/DQConfig/Index">数据质量配置列表</a>
                    --> <a href="/ModelConfig/EditColumn">模型字段配置</a> --> 模型扩展字段配置
                </div>
            </td>
            <td align="right">
                <div class="version">
                    <input type="button" class="button" value="查看模型字段" onclick="gotoUrl('/ModelConfig/EditColumn')"
                        style="width: 125px;" />
                    <input type="button" class="button" value="返回" onclick="gotoUrl('/ModelConfig/ConfigRecordList')" />
                </div>
                <div class="modelEdit">
                    <input type="button" class="button" value=" 添加行 " onclick="UpdateModel('');" />
                    <input type="button" class="button" value=" 删除行 " onclick="DeleteRow()" />
                </div>
            </td>
        </tr>
    </table>
    <div class="divGrid">
        <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
        </table>
    </div>
    <div id="msg">
    </div>
    <div class="modelEdit">
        <input type="button" class="button" value="上一步" onclick="back()" />
        <input type="button" class="button" value="保存" onclick="save()" />
        <input type="button" class="button" value="下一步" onclick="save('next')" />
    </div>
    <div id="editColumn">
        <table class="listTitle">
            <tr>
                <td width="15">
                    <img src="/images/edit.png" />
                </td>
                <td width="600">
                    模型视图配置 -> 编辑视图字段信息
                </td>
                <td align="right">
                    <input type="button" class="button" value="完成" onclick="saveView()" />
                </td>
            </tr>
        </table>
        <table border="0" class="searchArea" cellpadding="0" cellspacing="0" width="100%">
            <tr class="1">
                <td align="right" style="width: 108px;">
                    <span class="red">*</span>字段名称：
                </td>
                <td>
                    <input id="txt_GUID" type="hidden" />
                    <input id="txt_ColumnName" type="text" style="width: 400px;" />
                </td>
            </tr>
            <tr class="1">
                <td align="right">
                    字段描述：
                </td>
                <td>
                    <input id="txt_ColumnDescription" type="text" style="width: 400px;" />
                </td>
            </tr>
            <tr class="1">
                <td align="right">
                    允许空：
                </td>
                <td>
                    <input id="txt_ColumnIsNullable" type="checkbox" value="ok" checked="checked" />
                    <input id="txt_PK" type="hidden" />
                    <input id="txt_ColumnType" type="hidden" />
                </td>
            </tr>
            <tr class="1">
                <td align="right">
                    固定值：
                </td>
                <td>
                    <input type="text" id="txt_ColumnFixValue" style="width: 200px;" />
                </td>
            </tr>
            <tr class="1">
                <td align="right">
                    默认值：
                </td>
                <td>
                    <input type="text" id="txt_ColumnDefaultValue" style="width: 200px;" />
                    <input type="hidden" id="txt_WhereCondition" />
                    <input type="hidden" id="txt_Operator" />
                    <input type="hidden" id="txt_ColumnExpression" />
                    <input type="hidden" id="txt_ColumnScriptExtend" />
                </td>
            </tr>
        </table>
    </div>
    <div id="loading" class="loading">
        <img src="/Images/tree_loading.gif" />
    </div>
</div>
